
<style>
    .custom-checkbox{
        float: left;margin: 0 15px;
    }
    .custom-control-input{
        margin: 4px 4px 0 !important;
    }
    td{
        border: 1px solid rgb(57, 57, 57) !important;
    }
    th{
        border: 1px solid rgb(57, 57, 57) !important;color: #ffffff;font-weight: bold;text-align: center;background-color: grey;
    }
    .cbsg{
        background-color: white;
        height: 34px;width: 100%;
        margin-left: 0;
        border: 1px solid #d2d6de;
        line-height: 36px;
    }
</style>
<div class="container">
    @if(isset($role))
        <form action="/admin/roles/update/{{$role->id}}" method="post">
            <input type="hidden" name="id" value="{{ $role->id }}">
            <input type="hidden" name="_method" value="PUT">
            @else
                <form action="/admin/roles" method="post">
                    <input type="hidden" name="_method" value="POST">
                    @endif


                    <input type="hidden" name="_token" value="{{ csrf_token() }}">

                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="name">角色名字</label>
                                @if(isset($role))
                                    <input type="text" class="form-control" name="name" id="name" placeholder="请输入角色名字" value="{{ old('name', $role->name) }}">
                                @else
                                    <input type="text" class="form-control" name="name" id="name" placeholder="请输入角色名字" value="">
                                @endif
                            </div>
                            <div class="form-group">

                                <label for="desc">角色描述</label>
                                @if(isset($role))
                                    <input type="text" class="form-control" name="desc" id="desc" placeholder="请输入角色描述" value="{{ old('desc', $role->desc) }}">
                                @else
                                    <input type="text" class="form-control" name="desc" id="desc" placeholder="请输入角色描述" value="">
                                @endif
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">

                                <label for="slug">角色标记</label>
                                @if(isset($role))
                                    <input type="text" class="form-control" name="slug" id="slug" placeholder="请输入角色标记" value="{{ old('slug', $role->slug) }}">
                                @else
                                    <input type="text" class="form-control" name="slug" id="slug" placeholder="请输入角色标记" value="">
                                @endif
                            </div>
                            <label for="desc">账号状态</label>
                            <div class="row cbsg">
                                <div class="col-md-4"><div class="custom-control custom-radio custom-control-inline">
                                        @if(isset($role))
                                            <input type="radio" id="customRadioInline1" name="status" class="custom-control-input" {{ $role->status == 1 ? 'checked="checked"' :'' }} value="1">
                                        @else
                                            <input type="radio" id="customRadioInline1" name="status" class="custom-control-input" checked="checked" value="1">
                                        @endif
                                        <label class="custom-control-label" for="customRadioInline1">启用</label>
                                    </div></div>
                                <div class="col-md-4"> <div class="custom-control custom-radio custom-control-inline">
                                        @if(isset($role))
                                            <input type="radio" id="customRadioInline2" name="status" class="custom-control-input" {{ $role->status == 2 ? 'checked="checked"' :'' }}  value="2">
                                        @else
                                            <input type="radio" id="customRadioInline2" name="status" class="custom-control-input" value="2">
                                        @endif
                                        <label class="custom-control-label" for="customRadioInline2">停用</label>
                                    </div></div>
                            </div>
                        </div>
                    </div>

                    <div class='custom-control custom-checkbox'><input type='checkbox' class='custom-control-input' id='selectAll'>
                        <label class='custom-control-label' for='selectAll'>全选</label></div>
                    <table class="table table-bordered table-dark">
                        <thead>
                        <tr>
                            <th colspan="2" width="35%">菜单权限</th>
                            <th width="65%">操作权限</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php
                        $permission = [
                            'query'=>'查询',
                            'create'=>'新增',
                            'import'=>'导入',
                            'export'=>'导出',
                            'edit'=>'编辑',
                            'delete'=>'删除',
                        ];
                        $topMenu = \DB::table('admin_menu')->where('parent_id',0)
                            ->get();

                        if(isset($role)){
                            $plist = \DB::table('admin_role_permissions')->where('role_id',$role->id)->pluck('permission_id');
                        }else{
                            $plist = [];
                        }

                        $slugs = \DB::table('admin_permissions')->whereIn('id',$plist)->pluck('slug')->toArray();
                        $current_id = 0;

                        // 循环第一层 顶级目录
                        foreach ($topMenu as $Topkey => $topmenu){

                            // 先写一个一级菜单
                            $checked = in_array($topmenu->permission,$slugs) ? "checked='checked'" : '';
                            $checkbox =  "<div class='custom-control custom-checkbox'><input type='checkbox' data-id='$Topkey' class='custom-control-input ckbox ckbox-first menu-checkbox' $checked id='$topmenu->permission' name='permission[]' value='$topmenu->permission'>
                                    <label class='custom-control-label' for='$topmenu->permission'>$topmenu->title</label></div>";
                            echo "<tr><td>$checkbox</td><td></td><td></td></tr>";

                            $secMenu = DB::table('admin_menu')->where('parent_id',$topmenu->id)->get();
                            // 循环第二层
                            foreach ($secMenu as $secKey => $secmenu){

                                $checked = in_array($secmenu->permission,$slugs) ? "checked='checked'" : '';
                                $checkbox2 = "<div class='custom-control custom-checkbox'><input type='checkbox' data-id='$Topkey' data-content='$Topkey-$secKey' class='custom-control-input ckbox ckbox-$Topkey ckbox-second menu-checkbox' $checked id='$secmenu->permission' name='permission[]' value='$secmenu->permission'>
                                    <label class='custom-control-label' for='$secmenu->permission'>$secmenu->title</label></div>";

                                // 检查是否有三级菜单
                                $thirdMenu = DB::table('admin_menu')->where('parent_id',$secmenu->id)->get();

                                $btnCheckbox = '';
                                if(empty($thirdMenu->toArray())){
                                    foreach ($permission as $v => $k){

                                        $str = "<div class='custom-control custom-checkbox'>
                                        <input type='checkbox' class='custom-control-input ckbox ckbox-$Topkey ckbox-$Topkey-$secKey btn-checkbox' $checked id='$secmenu->permission$v' name='permission[]' value='$secmenu->permission.$v'>
                                     <label class='custom-control-label' for='$secmenu->permission$v'>$k</label></div>";
                                        // 不需要详细权限的页面
                                        $passList = ['clue.create','clients.create','moneyOut.create','feedback.create','stat.amount.client','users.create','threepart'];
                                        // 允许导入的
                                        $importList = ['clue','clients'];
                                        // 允许导出的
                                        $outList = ['feedback','dial','mission','clue','clients','sea','moneyOut'];
                                        if(! in_array($secmenu->permission, $passList)){

                                            if($v == 'import' && in_array($secmenu->permission, $importList)){
                                                $btnCheckbox .=  $str;
                                            }else if($v == 'export' && in_array($secmenu->permission, $outList)){
                                                $btnCheckbox .=  $str;
                                            }else{
                                                $btnCheckbox .=  $str;
                                            }
                                        }
                                    }

                                    // 线索的权限checkBox缺少客户转交和转化为客户
                                    if($secmenu->permission == 'clue'){
                                        $btnCheckbox .=  "<div class='custom-control custom-checkbox'>
                                        <input type='checkbox' class='custom-control-input ckbox ckbox-$Topkey  ckbox-$Topkey-$secKey btn-checkbox' $checked id='$secmenu->permission-zhuanjiao' name='permission[]' value='$secmenu->permission.zhuanjiao'>
                                     <label class='custom-control-label' for='$secmenu->permission-zhuanjiao'>客户转交</label></div>";
                                        $btnCheckbox .=  "<div class='custom-control custom-checkbox'>
                                        <input type='checkbox' class='custom-control-input ckbox ckbox-$Topkey  ckbox-$Topkey-$secKey btn-checkbox' $checked id='$secmenu->permission-zhuanhua' name='permission[]' value='$secmenu->permission.zhuanhua'>
                                     <label class='custom-control-label' for='$secmenu->permission-zhuanhua'>转化为客户</label></div>";
                                    }

                                    //  客户的权限checkBox缺少客户转交、放入公海和提交审核。
                                    if($secmenu->permission == 'clients'){
                                        $btnCheckbox .=  "<div class='custom-control custom-checkbox'>
                                        <input type='checkbox' class='custom-control-input ckbox ckbox-$Topkey  ckbox-$Topkey-$secKey btn-checkbox' $checked id='$secmenu->permission-zhuanjiao' name='permission[]' value='$secmenu->permission.zhuanjiao'>
                                     <label class='custom-control-label' for='$secmenu->permission-zhuanjiao'>客户转交</label></div>";
                                        $btnCheckbox .=  "<div class='custom-control custom-checkbox'>
                                        <input type='checkbox' class='custom-control-input ckbox ckbox-$Topkey  ckbox-$Topkey-$secKey btn-checkbox' $checked id='$secmenu->permission-gonghai' name='permission[]' value='$secmenu->permission.gonghai'>
                                     <label class='custom-control-label' for='$secmenu->permission-gonghai'>放入公海</label></div>";
                                        $btnCheckbox .=  "<div class='custom-control custom-checkbox'>
                                        <input type='checkbox' class='custom-control-input ckbox ckbox-$Topkey  ckbox-$Topkey-$secKey btn-checkbox' $checked id='$secmenu->permission-shenhe' name='permission[]' value='$secmenu->permission.shenhe'>
                                     <label class='custom-control-label' for='$secmenu->permission-shenhe'>提交审核</label></div>";
                                    }

                                    //  客户成交审核的权限checkBox缺少审核
                                    if($secmenu->permission == 'moneyOutCheck'){
                                        $btnCheckbox .=  "<div class='custom-control custom-checkbox'>
                                        <input type='checkbox' class='custom-control-input ckbox ckbox-$Topkey  ckbox-$Topkey-$secKey btn-checkbox' $checked id='$secmenu->permission-shenhe' name='permission[]' value='$secmenu->permission.shenhe'>
                                     <label class='custom-control-label' for='$secmenu->permission-shenhe'>审核</label></div>";
                                    }
                                    // dd($btnCheckbox);
                                }else{
                                    // 循环第三层
                                    foreach ($thirdMenu as $thKey => $thmenu){
                                        $checked = in_array($thmenu->permission,$slugs) ? "checked='checked'" : '';
                                        $btnCheckbox .= "<div class='custom-control custom-checkbox'>
                                        <input type='checkbox' class='custom-control-input ckbox ckbox-$Topkey ckbox-$Topkey-$secKey btn-checkbox' $checked id='$thmenu->permission' name='permission[]' value='$thmenu->permission'>
                                        <label class='custom-control-label' for='$thmenu->permission'>$thmenu->title</label></div>";
                                    }
                                }

                                echo "<tr><td></td><td>$checkbox2</td><td>$btnCheckbox</td></tr>";

                            }









                            // $checked = in_array($m->permission,$slugs) ? "checked='checked'" : '';
                            // $checkbox =  "<div class='custom-control custom-checkbox'><input type='checkbox' data-id='$key' class='custom-control-input ckbox ckbox-first menu-checkbox' $checked id='$m->permission' name='permission[]' value='$m->permission'>
                            //             <label class='custom-control-label' for='$m->permission'>$m->title</label></div>";
                            // if($m->parent_id == 0){
                            //     $current_id=$m->id;
                            //     echo "<tr><td>$checkbox</td><td></td><td></td></tr>";
                            // }else{
                            //     if($m->parent_id == $current_id){
                            //         // 权限里面没有 . 的，才给加按钮操作
                            //         $btnCheckbox = '';
                            //         if(! strstr($m->permission, '.')){
                            //             foreach ($permission as $v => $k){
                            //                 $btnCheckbox .=  "<div class='custom-control custom-checkbox'><input type='checkbox' class='custom-control-input ckbox ckbox-$key btn-checkbox' $checked id='$m->permission$v' name='permission[]' value='$m->permission.$v'>
                            //             <label class='custom-control-label' for='$m->permission$v'>$k</label></div>";
                            //             }
                            //         }else{ // 二级权限里面包含了 . 的
                            //             $items = collect($menu)->filter(function ($value,$key) use($m) {
                            //                 return $value->parent_id == $m->id ? true : false;
                            //             });
                            //             foreach ($items as $k => $item){
                            //                 $checked = in_array($item->permission,$slugs) ? "checked='checked'" : '';
                            //                 $btnCheckbox .=  "<div class='custom-control custom-checkbox'><input type='checkbox' class='custom-control-input ckbox ckbox-$key btn-checkbox' $checked id='$item->permission' name='permission[]' value='$item->permission'>
                            //             <label class='custom-control-label' for='$item->permission'>$item->title</label></div>";
                            //             }
                            //         }
                            //         echo "<tr><td></td><td>$checkbox</td><td>$btnCheckbox</td></tr>";
                            //     }
                            // }
                        }
                        ?>

                        </tbody>
                    </table>
                    <button class="btn btn-primary " type="submit"> 确定 </button>

                </form>
</div>

<script type="text/javascript">
    $("#selectAll").click(function () {
        if ($(this).prop("checked")) {
            $('.ckbox').attr('checked',true)
        } else {
            $('.ckbox').removeAttr('checked')
        }
    });

    $(document).on('change','.ckbox-first',function () {
        var id = $(this).data('id');
        var _this = $('.ckbox-'+id);
        if($(this).prop('checked')){
            _this.attr('checked',true);
        }else{
            _this.attr('checked', false);
        }
    })

    $(document).on('change','.ckbox-second',function () {
        var id = $(this).data('content');
        var _this = $('.ckbox-'+id);
        if($(this).prop('checked')){
            _this.attr('checked',true);
        }else{
            _this.attr('checked',false);
        }
    })
</script>
